package com.zm.demo.shoppingmall.order.service.impl;

import com.zm.demo.shoppingmall.order.api.OrderService;
import com.zm.demo.shoppingmall.order.dao.TOrderMapper;
import com.zm.demo.shoppingmall.order.model.TOrder;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

/**
 * @desc:
 * @author: z01140
 * @date: 2021/5/25 9:35
 */

@DubboService
public class OrderServiceImpl implements OrderService {

    @Autowired
    private TOrderMapper orderMapper;

    @Override
    public int addOrder(TOrder order) {
        LocalDateTime now = LocalDateTime.now();
        order.setCreateTime(now);
        order.setUpdateTime(now);
        Random r = new Random();
        int orderId = r.nextInt(9999);
        order.setOrderId(orderId);
        order.setOrderNo(String.valueOf(orderId));
        return orderMapper.insertSelective(order);
    }

    @Override
    public int updateOrder(TOrder order) {
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public int deleteOrder(Integer orderId) {
        return orderMapper.deleteByPrimaryKey(orderId);
    }

    @Override
    public TOrder getOrderByOrderNo(String orderNo) {
        return orderMapper.getOrderByOrderNo(orderNo);
    }

    @Override
    public List<TOrder> getOrderByUserId(int userId) {
        return orderMapper.getOrderByUserId(userId);
    }

    @Override
    public List<TOrder> getOrderByProductNo(String productNo) {
        return orderMapper.getOrderByProductNo(productNo);
    }
}
